package com.by.controller;

import com.by.auto.token.TokenTemplate;
import com.by.bean.User;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.bean.params.UserQuery;
import com.by.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
@Api(tags = "用户管理")
public class UserController extends BaseController {

    @Autowired
    TokenTemplate tokenTemplate;

    @Autowired
    UserService userService;

    /*
    @ApiImplicitParams({
          @ApiImplicitParam(name = "page",value = "当前第几页,默认显示第一页",dataTypeClass = Integer.class,paramType = "query"),
          @ApiImplicitParam(name = "pageSize",value = "每页记录数,默认10条",dataTypeClass = Integer.class,paramType = "query"),
          @ApiImplicitParam(name = "username",value = "用户名",dataTypeClass = String.class,paramType = "query")
    })*/
    @ApiOperation("查询用户")
    @PostMapping("/user/list")
    public ResultBean<PageBean<User>> query(@RequestBody UserQuery userQuery){
        PageBean bean = userService.query(userQuery.toMap());
        return success("用户列表",bean);
    }

    @ApiOperation("保存用户")
    @PostMapping("/user")
    public ResultBean save(@Validated @RequestBody User user, BindingResult result){
        validate(result);
        int rs = userService.save(user);
        return rs == 1? success("保存成功！"):success("保存失败！");
    }

    @ApiOperation("修改用户")
    @PutMapping("/user")
    public ResultBean update(@Validated @RequestBody User user, BindingResult result) {
        validate(result);
        int rs = userService.update(user);
        return rs == 1 ? success("更新成功") : fail("更新失败");
    }


    @ApiImplicitParam(name = "id",value = "用户id",defaultValue = "",required = true,dataTypeClass = String.class,paramType = "query")
    @ApiOperation("删除用户")
    @DeleteMapping("/user")
    public ResultBean delete(@RequestParam(name = "id",defaultValue = "",required = true) String id){

        return success("删除成功！");
    }

}
